package com.graduation_design.modules.app.controller;

import cn.hutool.core.util.StrUtil;
import com.graduation_design.common.lang.Result;
import com.graduation_design.entity.AppRefund;
import com.graduation_design.modules.app.annotation.Login;
import com.graduation_design.service.AppRefundService;
import com.graduation_design.service.AppUploadService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/app/refund")
public class RefundController extends BaseController{

    @Resource
    AppRefundService appRefundService;

    @Resource
    AppUploadService appUploadService;

    /**
     * 退款界面的基础数据
     */
    @Login
    @GetMapping("/order")
    public Result getOrderData(Long orderId) {
        return Result.success(appRefundService.getOrderData(orderId));
    }

    /**
     * 用户选择退款理由和方式之后发起退款
     */
    @Login
    @PostMapping("/apply")
    public Result applyRefund(MultipartFile[] pics, @Validated AppRefund appRefund) throws IOException {

        if (pics != null && pics.length > 0) {
            System.out.println("开始图片处理");
            List<String> paths = appUploadService.upload(pics, 0.5F, 0.3);
            appRefund.setImages(StrUtil.join( ";", paths));
        }

        appRefundService.apply(appRefund);
        return Result.success();
    }

}
